﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CivilleWP7.CivilleDataServiceReference;
using System.Windows.Media.Imaging;
using CivilleWP7.Civille.Source;
using System.Data.Services.Client;

namespace CivilleWP7.Views.Pages
{
    public partial class GetReward : PhoneApplicationPage
    {
        public static Reward TheReward;
        private bool IsLightTheme;
        public GetReward()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(GetReward_Loaded);
            setBackground();
        }
        void checkThemeColor()
        {
            var visibility = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"];
            if (visibility == Visibility.Visible)
                IsLightTheme = true;
            else
                IsLightTheme = false;
        }
        void setBackground()
        {
            checkThemeColor();
            ImageBrush myBrush = new ImageBrush();
            if(IsLightTheme)
                myBrush.ImageSource=new BitmapImage(new Uri("/Resources/Images/Backgroundwhite.jpg",UriKind.Relative));
            else
                myBrush.ImageSource=new BitmapImage(new Uri("/Resources/Images/Background.jpg",UriKind.Relative));
            ContentPanel.Background=myBrush;
        }

        void GetReward_Loaded(object sender, RoutedEventArgs e)
        {
            this.PageTitle.Text = TheReward.Title;
            this.DescriptionTxt.Text = TheReward.Description;
            this.LogoImage.Source = new BitmapImage(new Uri(TheReward.Logo, UriKind.Absolute));
        }

        private void confirmBtn_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to confirm? If you aren't at the partners location you cannot receive this reward anymmore", "Confirm", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                confirmBtn.IsEnabled = false;
                CivilleDataServiceConnector.SetConfirmRewardQueryParameters(TheReward.ID);
                CivilleDataServiceConnector.ConfirmRewardQuery.BeginExecute(OnConfirmQueryCompleted, CivilleDataServiceConnector.ConfirmRewardQuery);
            }
        }

        private void OnConfirmQueryCompleted(IAsyncResult result)
        {
            Dispatcher.BeginInvoke(() =>
                {
                    DataServiceQuery<CivilleUserToReward> query = result.AsyncState as DataServiceQuery<CivilleUserToReward>;
                    CivilleUserToReward entry = query.EndExecute(result).First();
                    CivilleDataServiceConnector.CivilleContext.DeleteObject(entry);
                    CivilleDataServiceConnector.CivilleContext.BeginSaveChanges(OnSavedChanges, CivilleDataServiceConnector.CivilleContext);
                });
        }

        private void OnSavedChanges(IAsyncResult result)
        {
            Dispatcher.BeginInvoke(() =>
                {
                    CivilleEntities entities = result.AsyncState as CivilleEntities;
                    entities.EndSaveChanges(result);
                });
        }
    }
}